package marksys.free.com.marksys.ui.face.facebykomlinplus;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector.Face;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

public class FaceView extends AppCompatImageView {
    private static final String TAG = "YanZi";
    private Context mContext;
    private Paint mLinePaint;
    private Face[] mFaces;


    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initPaint();
        mContext = context;

    }


    public void setFaces(Face[] faces){
        this.mFaces = faces;
        invalidate();
    }
    public void clearFaces(){
        mFaces = null;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // TODO Auto-generated method stub
        if(mFaces == null || mFaces.length < 1){
            return;
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);

        for(int i = 0; i< mFaces.length; i++){
            if (mFaces[i] ==null || mFaces[i].confidence()<=0.51){
                break;
            }
            PointF eyescenter = new PointF();
            mFaces[i].getMidPoint(eyescenter);
            float eyesdist = mFaces[i].eyesDistance();


            canvas.drawRect(
                    (int)(eyescenter.x - eyesdist),
                    (int)(eyescenter.y - eyesdist),
                    (int)(eyescenter.x + eyesdist),
                    (int)(eyescenter.y + eyesdist),
                    paint);


        }


    }

    private void initPaint(){
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//		int color = Color.rgb(0, 150, 255);
        int color = Color.rgb(98, 212, 68);
//		mLinePaint.setColor(Color.RED);
        mLinePaint.setColor(color);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(5f);
        mLinePaint.setAlpha(180);
    }

}
